package servlet;


import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import service.PricingService;
import util.ServiceFactory;

public class PricingServlet extends HttpServlet {
	@Override
	protected void service(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		//依据请求的不同，调用不同的model
		String uri = request.getRequestURI();
		String path = uri.substring(uri.lastIndexOf("/"),uri.lastIndexOf("."));
		PricingService service=ServiceFactory.getInstance(PricingService.class);
		String productName=request.getParameter("productName");
		double basePrice=Double.parseDouble(request.getParameter("basePrice"));
		try {
			double price=service.pricing(basePrice, productName);
			//依据model返回的结果，选择合适的视图
			if(price==-1){
				request.setAttribute("pricing_err", "taxRate not found");
				request.getRequestDispatcher("request.jsp").forward(request, response);
			}else{
				request.setAttribute("price", price);
				request.getRequestDispatcher("response.jsp").forward(request, response);
			}
		} catch (Exception e) {
			e.printStackTrace();
			throw new ServletException(e);
		}
	}
}
